#include <avr/pgmspace.h>
#include <WProgram.h>
#include <Wire.h>
#include <DS1307.h> 
#include <MemoryFree.h>
#include <LiquidCrystal.h>
#include <OneWire.h>
#include <DallasTemperature.h>
#include <Keypad.h>
#include <Streaming.h>
#include <EEPROM.h>

#include "utility.h";
#include "Aquarium_Devices.h";
#include "Dosing_Pumps.h";
#include "Clock.h";
#include "Temp.h";
#include "Gui.h";
#include "My_Keypad.h";
#include "Aquarium_Modes.h";
#include "Auto_Top.h";
#include "Alerts.h";



LiquidCrystal lcd(36,37,38,39,40,41);


Aquarium_Devices aquarium_devices = Aquarium_Devices();
Dosing_Pumps            dosing_pumps;
Aquarium_Modes       aquarium_modes;
Clock                               clock;
Temp                                  temp;
Gui                                     gui;
Auto_Top                       auto_top;
Alerts                            alerts;

void setup()			  
{
  lcd.begin(LCD_COLUMNS, LCD_LINES);
  //Serial.begin(9600);
  lcd.setCursor(0, 1);
  lcd.print("initialize clock");
  clock.init();
  aquarium_devices.update_devices();
  delay(1000);
  lcd.clear();
  lcd.setCursor(0, 1);
  lcd.print("initialize temp");  
  temp.init();
  delay(1000);
  lcd.clear();

  auto_top.init();
  //  pinMode(13, OUTPUT);  

}


void loop()			  
{

  clock.update_clock();
  dosing_pumps.update_Pumps(new_min,new_sec);
  auto_top.update(new_day );


  /*
  if( new_sec){
   if ( TBD){
   digitalWrite(13, HIGH);   // set the LED on
   TBD =!TBD;
   } 
   else {
   digitalWrite(13, LOW);   // set the LED on
   TBD =!TBD;
   }
   
   }
   */
  /*
   if( new_min& 0 ){//new_sec){
   digitalWrite(latchPin, LOW);          //Pull latch LOW to start sending data
   if ( TBD){
   shiftOut(dataPin, clockPin, MSBFIRST, B01010101);         //Send the data
   shiftOut(dataPin, clockPin, MSBFIRST, B01010101);         //Send the data
   TBD =!TBD;
   } 
   else {
   shiftOut(dataPin, clockPin, MSBFIRST, B10101010);         //Send the data
   shiftOut(dataPin, clockPin, MSBFIRST, B10101010);         //Send the data
   TBD =!TBD;
   }
   digitalWrite(latchPin, HIGH);         //Pull latch HIGH to stop sending data
   
   }
   */
  if( new_sec){
    temp.update_temp();
    gui.Update();
  }

  if ( new_min ){
    aquarium_modes.update();
    dosing_pumps.Check_Pumps();
    aquarium_devices.update_devices();
    //    alerts.Turn_Horn_on();
  }

  if (new_hr){
    aquarium_devices.Update_Outputs();
  }

  /* 
   Serial.print("freeMemory()=");
   Serial.println(freeMemory());
   
   
   
   if (Serial.available() > 0) {
   char key = Serial.read() ;
   gui.Key_Press(key  ,false);
   }
   */
  {
    char key = keypad.getKey();
    if (key != NO_KEY){
      if(key == 'D'){
        lcd.begin(LCD_COLUMNS, LCD_LINES);
      }
      gui.Key_Press(key  ,false);
    }
  }
}













